library(tidyverse)
library(readxl)
path = "Excel/654 Percentage Within a Subgroup.xlsx"
input = read_excel(path, range = "A1:C13")
test = read_excel(path, range = "D1:D13")
result = input %>%
mutate(`Answer Expected` = Revenue/sum(Revenue), .by = Group)
all.equal(result$`Answer Expected`, test$`Answer Expected`, check.attributes = FALSE)
#> [1] TRUEExcel BI - Excel Challenge 654
excel-challenges
excel-formulas
🔰 Work out the % Revenue share of each year within a group where % = Revenue / Sum of Revenue of a Group.

Challenge Description
🔰 Work out the % Revenue share of each year within a group where % = Revenue / Sum of Revenue of a Group. Ex. Sum of Group A revenue = 192, hence for row 2, % = 57/192 = 30%
Solutions
- Logic: Read the workbook ranges needed for the challenge; Derive the required intermediate columns; Aggregate or rank the data at the required grouping level.
- Strengths: The code maps the workbook rule into a compact, reproducible pipeline.
- Areas for Improvement: The solution assumes the workbook layout and selected ranges remain stable, so any structural change in the sheet would require small adjustments.
- Gem: The elegant part is how little code is needed once the correct intermediate representation is chosen.
import pandas as pd
path = "654 Percentage Within a Subgroup.xlsx"
input = pd.read_excel(path, usecols="A:C", nrows=12)
test = pd.read_excel(path, usecols="D", nrows=12)
input['Answer Expected'] = input.groupby('Group')['Revenue'].transform(lambda x: x / x.sum())
result = input['Answer Expected'].equals(test['Answer Expected'])
print(result) # TrueThe Python version follows the same grouped logic and keeps the transformation explicit in a dataframe pipeline.
Difficulty Level
Easy / Medium
The business rule is clear, though the workbook still needs a few transformation steps to reach the expected output.